import { defineStore } from 'pinia';

export const useParticipantStore = defineStore('participant', {
  state: () => ({
    participants: [], // ParticipantModel[]
    selectedAgentIdForLogView: null, // string | null
  }),
  getters: {
    getParticipantById: (state) => {
      return (participantId) => state.participants.find(p => p.participant_id === participantId);
    },
    llmAgents: (state) => {
      return state.participants.filter(p => p.role === 'llm_agent');
    },
    humanParticipant: (state) => {
      return state.participants.find(p => p.role === 'human');
    },
  },
  actions: {
    setParticipants(participants) {
      this.participants = participants || [];
    },
    updateParticipantStatus(participantId, status, desireToSpeak = undefined) {
      const participant = this.participants.find(p => p.participant_id === participantId);
      if (participant) {
        participant.status = status;
        if (desireToSpeak !== undefined && 'desire_to_speak' in participant) { // Check if property exists
            // This assumes desire_to_speak is a specific attribute for LLM agents
            // and might not exist on all participant models.
            // A more robust way would be to ensure the participant model has this attribute
            // or handle different participant types.
            participant.desire_to_speak = desireToSpeak;
        }
      } else {
        console.warn(`Participant with ID ${participantId} not found for status update.`);
      }
    },
    setSelectedAgentIdForLogView(agentId) {
      this.selectedAgentIdForLogView = agentId;
    },
    // Initial participant data fetching is handled in App.vue
    // and then setParticipants is called.
  },
});